package entity;

import com.hzheima.util.ImageUtil;
import constant.PlaneConstant;
import constant.UiConstant;
import entity.props.BulletBuff;
import interaction.crash.EntityCrash;
import javazoom.jl.player.Player;
import util.MusicUtil;

import java.awt.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author:zhengbowen
 * @Date: 2025/09/07 11:41
 * @Description:飞机类
 */
public class Plane extends FlyingObject{
    //飞机总血量
    private int blood;
    private int currentBlood = 10;
    //飞机的角色
    private int player;
    private int exp = 0;
    private HashMap<Integer, Integer> levelUpMap;
    private int level;
    //飞机的总血量
    //创建子弹集合
    public static ArrayList<Bullet> bullets = new ArrayList<>();
    //子弹个数
    int count;
    //闪烁是否开启
    private boolean lowHealthBlink;
    // 闪烁间隔200毫秒
    private long blinkTime;
    //升级提示
    private Toast toast;
    // 大招能量
    private int superSkillEnergy = 0;
    public static final int MAX_ENERGY = 10;
    public Plane(int player) {
        if (player==PlaneConstant.player1Id)
            image = ImageUtil.getImg("hero.png");
        else if (player==PlaneConstant.player2Id)
            image = ImageUtil.getImg("hero2.png");
        //设置大小
        width = image.getWidth() *2/5;
        height = image.getHeight()*2/5;
        //设置角色
        this.player=player;
        //设置经验
        levelUpMap=new HashMap<>(Map.ofEntries(
                Map.entry(1, 100),
                Map.entry(2, 500),
                Map.entry(3, 1500),
                Map.entry(4, 5000),
                Map.entry(5, 15000)
        ));
        level=PlaneConstant.FIRST_LEVEL;
        //设置飞机血量,初始化
        blood = PlaneConstant.PLANE_BLOOD;
        currentBlood = blood;
        //设置闪烁状态
        blinkTime = PlaneConstant.PLANE_PLASH_TIME;
        lowHealthBlink = PlaneConstant.PLANE_FLASH;
        if (player==2)
        {
            x = UiConstant.APP_WIDTH / 2 + width +30 ;
            y = UiConstant.APP_HEIGHT - height - 30;
        }
        else if (player==1){
            x = UiConstant.APP_WIDTH / 2 - width -150;
            y = UiConstant.APP_HEIGHT - height - 30;
        }
    }

    public void draw(Graphics p) {
        p.drawImage(this.image, this.x, this.y, this.width, this.height, null);
        this.drawLevelBar(p);
        this.drawSkillEnergyBar(p);
    }

    public void moveLeft() {
        if (x < 0) {
            return;
        }
        x -= xSpeed;
    }

    public void moveRight() {
        if (x > UiConstant.APP_WIDTH- width-20) {
            return;
        }
        x += xSpeed;
    }
    public void moveUp() {
        if (y < 0) {
            return;
        }
        y -= ySpeed;
    }

    public void moveDown() {
        if (y > UiConstant.APP_HEIGHT- height-40) {
            return;
        }
        y += ySpeed;
    }
    public ArrayList<Bullet> getBullets() {
        return bullets;
    }
    //发射子弹
    public void shoot() {
        count++;
        ArrayList<BulletBuff> bulletBuffs = EntityCrash.bulletBuffss;
        int size = bulletBuffs.size();
        if (size == 0) {
            count++;
            if (count % 5 == 0) {//控制火力
                //发射一颗子弹
                Bullet bullet = new Bullet(this.x + 45, this.y - 50,level);
                bullets.add(bullet);
                //count重置为0
                count = 0;
            }
            for (int i = 0; i < bullets.size(); i++) {
                Bullet bullet = bullets.get(i);
                bullet.move();
                if (bullet.getY() < 0) {
                    bullets.remove(bullet);
                }
            }

        }
        if (size == 1) {
            count++;
            if (count % 5 == 0) {//控制火力
                //发射一颗子弹
                Bullet bullet1 = new Bullet(this.x + 40, this.y - 50,level);
                Bullet bullet2 = new Bullet(this.x + 50, this.y - 50,level);

                bullets.add(bullet1);
                bullets.add(bullet2);
                //count重置为0
                count = 0;
            }
            for (int i = 0; i < bullets.size(); i++) {
                Bullet bullet = bullets.get(i);
                bullet.move();
                if (bullet.getY() < 0) {
                    bullets.remove(bullet);
                }
            }
        }
        if (size == 2) {
            count++;
            if (count % 5 == 0) {//控制火力
                //发射一颗子弹
                Bullet bullet1 = new Bullet(this.x + 35, this.y - 50,level);
                Bullet bullet2 = new Bullet(this.x + 45, this.y - 50,level);
                Bullet bullet3 = new Bullet(this.x + 55, this.y - 50,level);
                bullets.add(bullet1);
                bullets.add(bullet2);
                bullets.add(bullet3);
                //count重置为0
                count = 0;
            }
            for (int i = 0; i < bullets.size(); i++) {
                Bullet bullet = bullets.get(i);
                bullet.move();
                if (bullet.getY() < 0) {
                    bullets.remove(bullet);
                }
            }
        }
        if (size == 3) {
            count++;
            if (count % 5 == 0) {//控制火力
                //发射一颗子弹
                Bullet bullet1 = new Bullet(this.x + 30, this.y - 50,level);
                Bullet bullet2 = new Bullet(this.x + 40, this.y - 50,level);
                Bullet bullet3 = new Bullet(this.x + 50, this.y - 50,level);
                Bullet bullet4 = new Bullet(this.x + 60, this.y - 50,level);
                bullets.add(bullet1);
                bullets.add(bullet2);
                bullets.add(bullet3);
                bullets.add(bullet4);
                //count重置为0
                count = 0;
            }
            for (int i = 0; i < bullets.size(); i++) {
                Bullet bullet = bullets.get(i);
                bullet.move();
                if (bullet.getY() < 0) {
                    bullets.remove(bullet);
                }
            }
        }
        if (size >= 4) {
            count++;
            if (count % 5 == 0) {//控制火力
                //发射一颗子弹
                Bullet bullet1 = new Bullet(this.x + 25, this.y - 50,level);
                Bullet bullet2 = new Bullet(this.x + 35, this.y - 50,level);
                Bullet bullet3 = new Bullet(this.x + 45, this.y - 50,level);
                Bullet bullet4 = new Bullet(this.x + 55, this.y - 50,level);
                Bullet bullet5 = new Bullet(this.x + 65, this.y - 50,level);
                bullets.add(bullet1);
                bullets.add(bullet2);
                bullets.add(bullet3);
                bullets.add(bullet4);
                bullets.add(bullet5);
                //count重置为0
                count = 0;
            }
            for (int i = 0; i < bullets.size(); i++) {
                Bullet bullet = bullets.get(i);
                bullet.move();
                if (bullet.getY() < 0) {
                    bullets.remove(bullet);
                }
            }
        }
        //播放子弹发射音效
//        MusicUtil.playMp3("shoot.mp3");
    }

    public int getCurrentBlood() {
        return currentBlood;
    }

    public int getBlood() {
        return blood;
    }

    public void setBlood(int blood) {
        this.blood = blood;
    }

    public void setCurrentBlood(int currentBlood) {
        this.currentBlood = currentBlood;
    }

    public int getPlayer() {
        return player;
    }

    public void setPlayer(int player) {
        this.player = player;
    }

    public int getLevel() {
        return level;
    }

    public void setLevel(int level) {
        this.level = level;
    }

    //开启闪烁
    public void openFlash(Graphics p,Plane plane) {
        // 检查是否需要低血量闪烁
        checkLowHealthBlink();
        // 如果在闪烁状态且处于"关闭"阶段，跳过绘制
        if (lowHealthBlink && (System.currentTimeMillis() / blinkTime) % 2 == 0) {
            return; // 不绘制，实现闪烁效果
        }
        plane.draw(p);
    }
    // 检查低血量状态并更新闪烁
    private void checkLowHealthBlink() {
        if (currentBlood <= 2 && currentBlood > 0) {
            // 低血量时开始闪烁
            lowHealthBlink = true;
        } else {
            // 血量恢复时停止闪烁
            lowHealthBlink = false;
        }
    }
    //    获得经验
    public void eatExp(int score) {
        exp += score;
        if (exp >= levelUpMap.get(level)) {
            if (level < 5) {
                level++;
                exp = 0;
                toast = new Toast();
                toast.showMessage("等级提升至：" + level + "！");
            }
        }
    }
    public void drawLevelBar(Graphics p) {
//        画出自己的经验
        p.setColor(Color.GREEN);
        p.setFont(new Font("楷体", Font.BOLD, 16));
        p.drawString("当前等级：" + level, x + width / 2 - 40, y - 20);
        p.drawString("当前经验：" + exp + "/" + levelUpMap.get(level), x + width / 2 - 65, y);
//            升级提示
        if (toast != null && toast.isShowMessage()) {
            toast.drawLevelUpMessage(p);
        }
    }
    public void drawSkillEnergyBar(Graphics p){
        // 绘制大招能量条
        p.setColor(Color.gray);
        p.fillRect(20, UiConstant.APP_HEIGHT - 30, 200, 20);
        p.setColor(Color.blue);
        int energyWidth = (int) (((double) superSkillEnergy / MAX_ENERGY) * 200);
        p.fillRect(20, UiConstant.APP_HEIGHT - 30, energyWidth, 20);
        p.setColor(Color.white);
        p.setFont(new Font("楷体", Font.BOLD, 15));
        p.drawString("大招能量", 25, UiConstant.APP_HEIGHT - 15);
    }
    // 增加大招能量
    public void addSuperSkillEnergy(int amount) {
        superSkillEnergy += amount;
        if (superSkillEnergy > MAX_ENERGY) {
            superSkillEnergy = MAX_ENERGY;
        }
    }

    // 使用大招
    public boolean useSuperSkill() {
        if (superSkillEnergy >= MAX_ENERGY) {
            superSkillEnergy = 0;
            return true;
        }
        return false;
    }

    // 获取当前能量值
    public int getSuperSkillEnergy() {
        return superSkillEnergy;
    }

    // 获取最大能量值
    public int getMaxEnergy() {
        return MAX_ENERGY;
    }
}
